<?php

/**
 * Created by PhpStorm.
 * User: yangchunrun
 * Date: 17/3/22
 * Time: 下午6:07
 */
class response
{

    public static function reply($type, $keyword = '', $wechat = '')
    {

        global  $_G;
        loadcache('plugin');
        $splugin_setting = $_G['cache']['plugin']['thinker_wx'];
        if($type=='subscribe'){
            $query=$_G['setting']['subscribe_reply'];
        }else {
            if (is_numeric($keyword) && strlen($keyword)==4) {
                $row = C::t('#thinker_wx#common_attach_qrcode')->fetch("code1 = '{$keyword}' and  status=0");
                if ($row) {
                    $query = $row['code2'];
                }else{
                    $query = $splugin_setting['error_code'];
                }
            } else {
                $row = C::t('#thinker_wx#thinker_wx_keyword')->fetch_by_keyword($keyword);

                if (!$row) {
                    $match_list = C::t('#thinker_wx#thinker_wx_keyword')->fetch_all_list("*", "status=1 and is_match=1", "order_num");
                    foreach ($match_list as $k => $v) {
                        if (strpos($keyword,$v['keyword']) !== FALSE||strpos($v['keyword'],$keyword) !== FALSE) {
                            $children = C::t('#thinker_wx#thinker_wx_keyword')->fetch_one("*", "parent_id ={$v['id']} and status=1", "order_num");
                            if($children){
                                $query = $children['text'];
                            }else {
                                $query = $v['text'];
                            }
                            break;
                        }
                    }

                } else {
                    $children = C::t('#thinker_wx#thinker_wx_keyword')->fetch_one("*", "parent_id ={$row['id']} and status=1", "order_num");
                    if($children){
                        $query = $children['text'];
                    }else {
                        $query = $row['text'];
                    }
                }
            }
        }

        if(!$query){
            $wechat->text($splugin_setting['no_keyword'])->reply();
        }

        if (preg_match("/^\[resource=(\d+)\]/", $query, $r)) {
            $resource = C::t('#thinker_wx#thinker_wx_resource')->fetch($r[1]);
            if (!$resource['type']) {
                $list = array(array(
                    'Title' => $resource['data']['title'],
                    'Description' => $resource['data']['desc'],
                    'PicUrl' => $resource['data']['pic'],
                    'Url' => $resource['data']['url'],
                ));
            } else {
                $mergeids = array_keys($resource['data']['mergeids']);
                $sresource = C::t('#wechat#mobile_wechat_resource')->fetch_all($mergeids);
                $list = array();
                foreach ($resource['data']['mergeids'] as $id => $order) {
                    $list[] = array(
                        'Title' => $sresource[$id]['data']['title'],
                        'Description' => $sresource[$id]['data']['desc'],
                        'PicUrl' => $sresource[$id]['data']['pic'],
                        'Url' => $sresource[$id]['data']['url'],
                    );
                }
            }

            $wechat->news($list)->reply();
        } else {
            $wechat->text($query)->reply();

        }
    }
}